﻿jQuery(document).ready(function () {
    //CollapseLeftMenu();
    SetCurrentPage_Top();
    SetCurrentItem_left();
    watermark();
    scrollwindow();
    setHeight();
    $('#leftmenu div.title').click(function() {
        var self = $(this);
        self.next().slideToggle("fast");
    });

    //$('.boxgrid').each(function () {
    //    var $caption = $(this).find('div.boxcaption');
    //    var pheight = $(this).height();
    //    var topCaption = pheight - $caption.height();
    //    $caption.css('top', topCaption);
    //});
    $(".viewdetail a").mouseover(function () {
        $(this).find("img").attr('src', '/images/out_bound_icon_hover.png');
    }).mouseout(function () {
        $(this).find("img").attr('src', '/images/out_bound_icon.png');
    });

    jQuery("#txtsearch").on('keydown', function (e) {
        var keycode = e.which;
        if (keycode == 13) {
            window.location.href = "/tim-kiem/" + jQuery(this).val()+".html";
        }
    });

    jQuery("#sendcontact").click(function () {
        SendContact();
    });
});

function CollapseLeftMenu(callback) {
    var items = jQuery("#leftmenu ul:visible");
    if(items.length) {
        items.slideToggle("fast", callback);
    }
    else {
        if(typeof callback == "function") {
            callback();
        }
    }
}

function SendContact() {
    var validateForm = jQuery("#frmcontact");
    validateForm.validate({
        submitHandler: function () {
            return false;
        }
    });
    if (validateForm.valid()) {
        jQuery("#msgboxContact").html("<img src='/images/loading.gif' alt='loading...' /><br />Đang gửi thông tin");
        jQuery.ajax({
            method: "POST",
            url: "/Ajax.aspx",
            data: jQuery("#frmcontact").serialize(),
            success: function (response) {
                jQuery("#msgboxContact").html("");
                jQuery("#msgboxContact").html(response);
            }
        });
    }
}

function watermark() {
    $(":input[data-watermark]").each(function () {
        $(this).val($(this).attr("data-watermark"));
        $(this).css("color", "#ccc");
        $(this).css("font-style", "italic");
        $(this).bind("focus", function () {
            if ($(this).val() == $(this).attr("data-watermark")) {
                $(this).val('');
            }
            $(this).css("color", "#ccc");
            $(this).css("font-style", "italic");
        });
        $(this).bind("blur", function () {
            if ($(this).val() == '') {
                $(this).val($(this).attr("data-watermark"));
                $(this).css("color", "#ccc");
            }
            else {
                $(this).css("color", "#000");
                $(this).css("font-style", "normal");
            }
        });
    });
}

function SetCurrentPage_Top() {
    var currentPage = "/" + window.location.pathname.split("/")[1];
    RemoveCurrentItem("#myslidemenu ul li");
    AddCurrentItem("#myslidemenu ul li", currentPage);
}

function SetCurrentItem_left() {
    var pathname = window.location.pathname;
    if (pathname.split("/").length > 1) {
        var lastItemLength = pathname.substr(pathname.lastIndexOf('/')).length;
        var currentPage = "";
        if (pathname.split("/").length > 5) {
            currentPage = pathname.substr(0, pathname.length - lastItemLength);
        }
        else {
            currentPage = pathname.substr(0, pathname.length);
        }
        RemoveCurrentItem("#leftmenu ul li");
        AddCurrentItem("#leftmenu ul li", currentPage);


        $("#leftmenu ul li").each(function (index) {
            if (window.location.pathname.indexOf($("#leftmenu ul li:eq(" + index + ")").attr("rel")) > 0) {
                $("#leftmenu ul li:eq(" + index + ")").addClass("current");
            }
        });
    }
}

function RemoveCurrentItem(element) {
    jQuery(element).each(function () {
        jQuery(element).find("a").removeClass("current");
    });
}

function AddCurrentItem(element, currentPage) {
    jQuery(element).each(function () {
        var url = jQuery(this).find("a").attr("href");
        if (url == currentPage) {
            jQuery(this).find("a").addClass("current");
        }
    });
}

function scrollwindow() {
    $(window).scroll(function () {
        var show = $(window).scrollTop();
        if (show < 700) {
            $('#top').hide(1000);
        } else {
            $('#top').show(1000);
        }
    });

    $('#top').click(function () {
        $('html, body').animate({ scrollTop: 0 }, 500);
    });
}
function setHeight() {
    var leftHeight = $(".leftcontainer").height();
    var rightHeight = $(".rightcontainer").height();
    if (leftHeight < rightHeight) {
        $(".leftcontainer").css("height", rightHeight);
    }
}